import json

file_path = "11112222.json"


# def change_to_d(file_path, r_c):
#     with open(file_path) as f:
#         content = json.load(f)

def change_to_d(content, r_c):
    items = content.get("data")
    controls = []
    r_item = {}
    for item in items:
        group = item.get("group")
        if group == "connect":
            controls = item.get("controls")
            r_item = item
            break
    values = {
        "uart1_baudrate": "",
        "uart1_device": "",
        "uart1_sensor_id": "",
        "uart2_baudrate": "",
        "uart2_device": "",
        "uart2_sensor_id": "",
        "uart3_baudrate": "",
        "uart3_device": "",
        "uart3_sensor_id": ""
    }
    for value_key in values:
        for item in controls:
            if item.get("name") == value_key:
                values[value_key] = item.get("value")
    for value_key in values:
        head, end = value_key.split("_")[0], value_key.split("_")[-1]
        if end == "id":
            break
        for item in r_c:
            if item.get("name") == head and item.get("attribute") == end:
                item["value"] = values[value_key]
            elif item.get("name") == head and item.get("attribute") == "device" and item.get("sensor"):
                item["value"] = values[value_key]
                new_key = "{}{}".format(head, "_sensor_id")
                item["sensor"]["value"] = values[new_key]
    r_item["controls"] = r_c
    return content


r_c = [
    {
        "name": "uart1",
        "attribute": "baudrate",
        "value": "115200",
        "text": "串口1波特率",
        "type": "combo",
        "option": [
            {
                "value": "9600",
                "label": "9600"
            },
            {
                "value": "19200",
                "label": "19200"
            },
            {
                "value": "57600",
                "label": "57600"
            },
            {
                "value": "115200",
                "label": "115200"
            },
            {
                "value": "230400",
                "label": "230400"
            }
        ]
    },
    {
        "name": "uart1",
        "attribute": "device",
        "value": "sensor",
        "text": "串口1装置",
        "type": "combo",
        "option": [
            {
                "value": "unused",
                "label": "unconnect"
            },
            {
                "value": "sensor",
                "label": "sensor"
            },
            {
                "value": "gateway",
                "label": "gateway"
            }
        ],
        "sensor": {
            "value": "1-8",
            "id": [
                1,
                2,
                3
            ],
            "text": "传感器ID",
            "type": "text",
            "regular": ""
        }
    },
    {
        "name": "uart2",
        "attribute": "baudrate",
        "value": "115200",
        "text": "串口2波特率",
        "type": "combo",
        "option": [
            {
                "value": "9600",
                "label": "9600"
            },
            {
                "value": "19200",
                "label": "19200"
            },
            {
                "value": "57600",
                "label": "57600"
            },
            {
                "value": "115200",
                "label": "115200"
            },
            {
                "value": "230400",
                "label": "230400"
            }
        ]
    },
    {
        "name": "uart2",
        "attribute": "device",
        "value": "sensor",
        "text": "串口2装置",
        "type": "combo",
        "option": [
            {
                "value": "unused",
                "label": "unconnect"
            },
            {
                "value": "sensor",
                "label": "sensor"
            },
            {
                "value": "gateway",
                "label": "gateway"
            }
        ],
        "sensor": {
            "value": "",
            "id": [],
            "text": "传感器ID",
            "type": "text",
            "regular": ""
        }
    },
    {
        "name": "uart3",
        "attribute": "baudrate",
        "value": "115200",
        "text": "串口3波特率",
        "type": "combo",
        "option": [
            {
                "value": "9600",
                "label": "9600"
            },
            {
                "value": "19200",
                "label": "19200"
            },
            {
                "value": "57600",
                "label": "57600"
            },
            {
                "value": "115200",
                "label": "115200"
            },
            {
                "value": "230400",
                "label": "230400"
            }
        ]
    },
    {
        "name": "uart3",
        "attribute": "device",
        "value": "sensor",
        "text": "串口3装置",
        "type": "combo",
        "option": [
            {
                "value": "unused",
                "label": "unconnect"
            },
            {
                "value": "sensor",
                "label": "sensor"
            },
            {
                "value": "gateway",
                "label": "gateway"
            }
        ],
        "sensor": {
            "value": "",
            "id": [
                6
            ],
            "text": "传感器ID",
            "type": "text",
            "regular": ""
        }
    },
    {
        "name": "ai",
        "attribute": "device",
        "value": "sensor",
        "text": "ai",
        "type": "combo",
        "option": [
            {
                "value": "unused",
                "label": "unconnect"
            },
            {
                "value": "sensor",
                "label": "sensor"
            }
        ]
    },
    {
        "name": "di",
        "attribute": "device",
        "value": "sensor",
        "text": "di",
        "type": "combo",
        "option": [
            {
                "value": "unused",
                "label": "unconnect"
            },
            {
                "value": "sensor",
                "label": "sensor"
            }
        ]
    },
    {
        "name": "do",
        "attribute": "device",
        "value": "sensor",
        "text": "do",
        "type": "combo",
        "option": [
            {
                "value": "unused",
                "label": "unconnect"
            },
            {
                "value": "sensor",
                "label": "sensor"
            }
        ]
    }
]

